﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Products;
using MinhTuan;

public partial class product_productdetail : System.Web.UI.Page
{
    Product product = new Product();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["id"] != null)
            {
                Binddata();
            }
            else
            {
                Response.Redirect("http://localhost:2011/didong/product/san-pham-khong-ton-tai.html");
            }
        }
    }
    private void Binddata()
    {
        int id = int.Parse(Request.QueryString["id"].Split('-')[0]);
        Session["product_Id"] = id;
        product = Product.GetProductdetail(id);
        Ltname.Text = product.Products_Name;
        Ltbaohanh.Text = product.Products_Warranty;
        Ltorgin.Text = product.Products_Origin;
        Ltthongtinchitiet.Text = product.Products_Detail;
        Ligiaban.Text = string.Format("{0:0,0}", product.Products_Price);
        Hdid.Value = "-"+ product.Products_Id.ToString()+",1";
        fancy.NavigateUrl = "http://localhost:2011/didong/" + product.Products_Image;
        Image1.ImageUrl = "http://localhost:2011/didong/" + product.Products_Image;
        this.Lvitem.DataSource = Product.GetProductSameBrand(product.Brand_Id, product.Products_Id);
        Lvitem.DataBind();
        ListComment.DataSource = MinhTuan.Comment.GetComment(id);
        ListComment.DataBind();
    }
    
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        validation();
        if (string.Compare(txtcapcha.Text, Session["strcapcha"].ToString(), true) == 0)
        {
            lblmessage.Text = "";
            Comment comment = new Comment();
            comment.UserName = Txtname.Text;
            comment.Email = Txtemail.Text;
            comment.Title = Txttitle.Text;
            comment.Detail = txtcontent.Text;
            comment.Product_Id = int.Parse(Session["product_Id"].ToString());
            Boolean result = Comment.Save(comment);
            if (result == true)
            {
                ListComment.DataSource = MinhTuan.Comment.GetComment(int.Parse(Session["product_Id"].ToString()));
                ListComment.DataBind();
                resetForm();
            }
           
        }
        else
        {
            lblmessage.Text="Mã xác nhận không đúng";
            txtcapcha.Text = "";
        }

    }
    protected void resetForm()
    {
        txtcapcha.Text = "";
        txtcontent.Text = "";
        Txtemail.Text = "";
        Txtname.Text = "";
        Txttitle.Text = "";

    }
    protected void btncarts_Click(object sender, EventArgs e)
    {

        if (Session["Shopcart"] == null)
        {
            Session["Shopcart"] = Hdid.Value.ToString();
            Response.Redirect("http://localhost:2011/didong/shoping/Viewcart.aspx");

        }
        else
        {
            String cart = Session["Shopcart"].ToString();
            Session["Shopcart"] = cart + Hdid.Value.ToString();
            Response.Redirect("http://localhost:2011/didong/shoping/Viewcart.aspx");
        }

        Page.ClientScript.RegisterStartupScript(this.GetType(), "redirect", "location.href = 'http://localhost:2011/didong/product'", true);
    }
    protected void validation()
    {
        if (Txtemail.Text.Trim().Length == 0 || Txtname.Text.Trim().Length == 0 || txtcontent.Text.Trim().Length == 0 || Txttitle.Text.Trim().Length == 0)
        {
            lblmessage.Text = "Vui lòng cung cấp đẩy đủ thông tin ";
            return;
        }
    }
}
